package org.csu.linkgame.controller;

import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.SortedList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import org.csu.linkgame.domain.User;
import org.csu.linkgame.service.ClassForGame.Clock;
import org.csu.linkgame.service.ClassForGame.testView;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Comparator;
import java.util.Optional;
import java.util.ResourceBundle;

import static javafx.application.Application.launch;

@FXMLController
public class SoloViewController implements Initializable  {
    @FXML
    public Button chooseModelButton;
    @FXML
    public Button promptButton;

    @FXML
    public AnchorPane gameWindow;
    @FXML
    public ImageView userInfo;
    User user;
    @FXML
    public TableColumn username;
    @FXML
    public TableColumn user_goal;
    @FXML
    public TableColumn user_rank;
    @FXML
    public TableView tableView;
    SortedList<User> sortedList;
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/super_link?useUnicode=true&characterEncoding=utf8";


    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "2003052288mjp";
    final ObservableList<User> data = FXCollections.observableArrayList();
    public void setUser(User user) {
        this.user = user;
    }
    public void setUserInfoImage() throws Exception{
        Image image = new Image("http://orange-1312206514.cos.ap-guangzhou.myqcloud.com/images/"+user.getUsername()+".jpg", 487,
                207, true, true);
        userInfo.setImage(image);
        // 进行数据绑定
        username.setCellValueFactory(
                new PropertyValueFactory<User,String>("username")
        );
        user_goal.setCellValueFactory(
                new PropertyValueFactory<User,String>("userGoal")
        );
        user_rank.setCellValueFactory(
                new PropertyValueFactory<User,String>("userRank")
        );
        // 连接数据库
        Connection conn = null;
        Statement stmt = null;
        Class.forName(JDBC_DRIVER);
        conn = DriverManager.getConnection(DB_URL,USER,PASS);

        stmt = conn.createStatement();
        String sql;
        //sql = "select username,user_goal,user_rank from user";
        //sql = "select * from user order by user_rank";
        sql = "select * from user";
        ResultSet rs = stmt.executeQuery(sql);
        while(rs.next()){
            data.add(new User(rs.getString(1),rs.getString(2)
                    ,rs.getString(3),rs.getInt(4),rs.getString(5)
                    ,rs.getString(6),rs.getString(7),rs.getString(8)));
        }
        SortedList<User> sl=data.sorted(new Comparator<User>() {//干嘛的我也不知道 官方给的方法  排序用
            @Override
            public int compare(User o1, User o2) {
                int a=Integer.parseInt(o1.getUserRank());//从小到大排序
                int b=Integer.parseInt(o2.getUserRank());
                return a-b;
            }
        });
        this.sortedList = sl;
        tableView.setItems(this.sortedList);
        // 完成后关闭
        conn.close();
        stmt.close();
    }
/*    extends Application
    @FXML
    private Label remindLabel;
    @FXML
    private Button sampleButton;*/
    @Override
    public void initialize(URL location, ResourceBundle resources) {
/*        // 获取当前窗口
        Stage window = (Stage) sampleButton.getScene().getWindow();
        try {
            this.start(window);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }*/
    }
    public void onChooseModelButtonClicked(MouseEvent mouseEvent) throws Exception {
// 选择难度
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("难度选择");
        alert.setHeaderText("请在下方按钮选择你要进行的游戏难度");
        alert.setContentText("分为高级、中级、初级");

        ButtonType buttonTypeOne = new ButtonType("高级");
        ButtonType buttonTypeTwo = new ButtonType("中级");
        ButtonType buttonTypeThree = new ButtonType("初级");
        ButtonType buttonTypeCancel = new ButtonType("Cancel", ButtonBar.ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeTwo, buttonTypeThree, buttonTypeCancel);

        int level = 0;
        Optional<ButtonType> result = alert.showAndWait();
        // 获取当前窗口
        Stage window = (Stage) chooseModelButton.getScene().getWindow();
        if (result.get() == buttonTypeOne){
            Clock.tmp=0;
            Clock.flag=0;
            Clock.animation.stop();
            // ... user chose "One"
            level = 2;
            Clock.flag = 1;
            gameWindow.getChildren().clear();
            // 打开单人模式界面
            testView open  = new testView(level,gameWindow);
            open.start(window);
        } else if (result.get() == buttonTypeTwo) {
            Clock.tmp=0;
            Clock.flag=0;
            Clock.animation.stop();
            // ... user chose "Two"
            level = 1;
            Clock.flag = 1;

            // 打开单人模式界面
            gameWindow.getChildren().clear();
            testView open  = new testView(level,gameWindow);
            open.start(window);
        } else if (result.get() == buttonTypeThree) {
            Clock.tmp=0;
            Clock.flag=0;
            Clock.animation.stop();
            // ... user chose "Three"
            level = 0;
            Clock.flag = 1;

            // 打开单人模式界面
            gameWindow.getChildren().clear();
            testView open  = new testView(level,gameWindow);
            open.start(window);
        } else {
            Clock.tmp=0;
            Clock.flag=0;
            Clock.animation.stop();
            //gameWindow.getChildren().clear();
            System.out.println("关闭窗口");
        }
    }



    /**
     * @author ylnx
     *
     */
    //public class Main extends Application {
        // 背景音乐(魔兽进行曲)
    /*AudioClip bgSound = AudioClipBuilder.create()
            .source(getClass().getResource("audios/war3.mp3").toExternalForm())
            .cycleCount(Integer.MAX_VALUE).build();*/



}
